import type { Admin } from '@/views/Login/type'

// const user = localStorage.getItem('user')
// const user = JSON.parse(localStorage.getItem('user') as string)
// TODO:为啥这里统一获取user不能达到预想的效果呢？？
// TODO:后面可以转移到store中去

// 本地存储admin信息
export const setAdmin = (admin: Admin) => {
  localStorage.setItem('user', JSON.stringify(admin))
}

// 清除本地信息
export const removeAdmin = () => {
  localStorage.removeItem('user')
}

// 获取user
export const getUser = () => {
  const user = JSON.parse(localStorage.getItem('user') as string)
  return user
}

// 获取token
export const getToken = () => {
  // const user = localStorage.getItem('user')
  // return JSON.parse(user as string)?.token
  const user = JSON.parse(localStorage.getItem('user') as string)
  return user?.token
}

// 清除token
export const clearAdminToken = () => {
  const user = JSON.parse(localStorage.getItem('user') as string)
  user.token = ''
  // 这里得重新更新localStorage中的user值才能使token=''凑效
  setAdmin(user)
}

// 获取用户名
export const getAdminName = () => {
  const user = JSON.parse(localStorage.getItem('user') as string)
  // return JSON.parse(user as string)?.name
  return user?.name
}

// 获取用户名密码
export const getAdminPassword = () => {
  const user = JSON.parse(localStorage.getItem('user') as string)
  // return JSON.parse(user as string)?.password
  return user?.password
}
